
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="案件ID:" prop="caseId">
        <el-select  v-model="formData.caseId" placeholder="请选择案件ID" style="width:100%" :clearable="true" >
          <el-option v-for="(item,key) in dataSource.caseId" :key="key" :label="item.label" :value="item.value" />
        </el-select>
       </el-form-item>
       <el-form-item label="案件名:" prop="caseName">
        <el-input v-model="formData.caseName" :clearable="true"  placeholder="请输入案件名" />
       </el-form-item>
        <el-form-item label="罪名:" prop="charge">
           <el-select v-model="formData.charge" placeholder="请选择罪名" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in suspectOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="逮捕罪名:" prop="apprehendCharge">
           <el-select v-model="formData.apprehendCharge" placeholder="请选择逮捕罪名" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in suspectOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="姓名:" prop="name">
          <el-input v-model="formData.name" :clearable="true"  placeholder="请输入姓名" />
       </el-form-item>
        <el-form-item label="头像:" prop="avatar">
          <el-input v-model="formData.avatar" :clearable="true"  placeholder="请输入头像" />
       </el-form-item>
        <el-form-item label="身份证:" prop="idNumber">
          <el-input v-model="formData.idNumber" :clearable="true"  placeholder="请输入身份证" />
       </el-form-item>
        <el-form-item label="地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true"  placeholder="请输入地址" />
       </el-form-item>
        <el-form-item label="年纪:" prop="age">
          <el-input v-model.number="formData.age" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="民族:" prop="nation">
           <el-select v-model="formData.nation" placeholder="请选择民族" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in nationOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="文化程度:" prop="culture">
           <el-select v-model="formData.culture" placeholder="请选择文化程度" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in cultureOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="乡镇:" prop="townCode">
           <el-select v-model="formData.townCode" placeholder="请选择乡镇" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="电话:" prop="phone">
          <el-input v-model="formData.phone" :clearable="true"  placeholder="请输入电话" />
       </el-form-item>
        <el-form-item label="主办人:" prop="fOwnerId">
        <el-select  v-model="formData.fOwnerId" placeholder="请选择主办人" style="width:100%" :clearable="true" >
          <el-option v-for="(item,key) in dataSource.fOwnerId" :key="key" :label="item.label" :value="item.value" />
        </el-select>
       </el-form-item>
        <el-form-item label="状态:" prop="state">
           <el-select v-model="formData.state" placeholder="请选择状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in suspect_stateOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="附件:" prop="attachments">
           <SelectImage v-model="formData.attachments" multiple file-type="image"/>
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
    getCasSuspectsDataSource,
  createCasSuspects,
  updateCasSuspects,
  findCasSuspects
} from '@/api/suspect/casSuspects'

defineOptions({
    name: 'CasSuspectsForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 图片选择组件
import SelectImage from '@/components/selectImage/selectImage.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const suspect_stateOptions = ref([])
const townOptions = ref([])
const nationOptions = ref([])
const suspectOptions = ref([])
const cultureOptions = ref([])
const formData = ref({
            caseId: undefined,
            caseName: '',
            charge: '',
            apprehendCharge: '',
            name: '',
            avatar: '',
            idNumber: '',
            address: '',
            age: undefined,
            nation: '',
            culture: '',
            townCode: '',
            phone: '',
            fOwnerId: undefined,
            state: '',
            attachments: [],
        })
// 验证规则
const rule = reactive({
               caseName : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               charge : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               avatar : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               idNumber : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               address : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               age : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               nation : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               culture : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               townCode : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               fOwnerId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               state : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()
  const dataSource = ref([])
  const getDataSourceFunc = async()=>{
    const res = await getCasSuspectsDataSource()
    if (res.code === 0) {
      dataSource.value = res.data
    }
  }
  getDataSourceFunc()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findCasSuspects({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    suspect_stateOptions.value = await getDictFunc('suspect_state')
    townOptions.value = await getDictFunc('town')
    nationOptions.value = await getDictFunc('nation')
    suspectOptions.value = await getDictFunc('suspect')
    cultureOptions.value = await getDictFunc('culture')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createCasSuspects(formData.value)
               break
             case 'update':
               res = await updateCasSuspects(formData.value)
               break
             default:
               res = await createCasSuspects(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
